package com.regexdemo;

public class RegexDemo2 {
    public static void main(String[] args) {
        //public boolean matches(string regex):判断是否与正则表达式匹配，匹配返回true

        //只能是a b c
        System.out.println("======1======");
        System.out.println("a".matches("[abc]"));//true
        System.out.println("z".matches("[abc]"));//false
        System.out.println("ab".matches("[abc]"));//false
        System.out.println("ab".matches("[abc][abc]"));//true

        //不能是a b c
        System.out.println("======2======");
        System.out.println("a".matches("[^abc]"));//false
        System.out.println("z".matches("[^abc]"));//true
        System.out.println("zz".matches("[^abc]"));//false
        System.out.println("zz".matches("[^abc][^abc]"));//true

        //a~z A~Z
        System.out.println("======3======");
        System.out.println("a".matches("[a-zA-Z]"));//true
        System.out.println("z".matches("[a-zA-Z]"));//true
        System.out.println("aa".matches("[a-zA-Z]"));//false
        System.out.println("zz".matches("[a-zA-Z]"));//false
        System.out.println("zz".matches("[a-zA-Z][a-zA-Z]"));//true
        System.out.println("0".matches("[a-zA-Z]"));//false
        System.out.println("0".matches("[^a-zA-Z]"));//true

        //a~d m~p
        System.out.println("======4======");
        System.out.println("a".matches("[a-d[m-p]]"));//true
        System.out.println("d".matches("[a-d[m-p]]"));//true
        System.out.println("m".matches("[a-d[m-p]]"));//true
        System.out.println("p".matches("[a-d[m-p]]"));//true
        System.out.println("e".matches("[a-d[m-p]]"));//false
        System.out.println("0".matches("[a-d[m-p]]"));//false

        //a~z 和 def 的交集为def
        ///细节:如果要求两个范围的交集，那么需要写符号&&
        ///如果写成了一个&，那么此时&表示就不是交集了，而是一个简简单单的&符号
        System.out.println("======5======");
        System.out.println("a".matches("[a-z&[def]]"));//true
        System.out.println("&".matches("[a-z&[def]]"));//true
        System.out.println("&".matches("[a-z&&[def]]"));//false
        System.out.println("a".matches("[a-z&&[def]]"));//false
        System.out.println("d".matches("[a-z&&[def]]"));//true
        System.out.println("e".matches("[a-z&&[def]]"));//true

        //a~z 和 非bc 的交集为[ad-z]
        System.out.println("======6======");
        System.out.println("a".matches("[a-z&&[^bc]]"));//true
        System.out.println("b".matches("[a-z&&[^bc]]"));//false
        System.out.println("0".matches("[a-z&&[^bc]]"));//false

        //a~z 和 非m~p 的交集为[a-lq-z]
        System.out.println("======7======");
        System.out.println("a".matches("[a-z&&[^m-p]]"));//true
        System.out.println("m".matches("[a-z&&[^m-p]]"));//false
        System.out.println("0".matches("[a-z&&[^m-p]]"));//false
    }
}
